import ai.djl.MalformedModelException;
import ai.djl.Model;
import ai.djl.inference.Predictor;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.Criteria;
import ai.djl.translate.NoBatchifyTranslator;
import ai.djl.translate.TranslatorContext;
import breeze.linalg.Tensor;
import com.alibaba.fastjson.JSONObject;
import org.ansj.splitWord.analysis.ToAnalysis;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test {
    public static void main(String[] args) {
//        String moves = "UD";
//        System.out.println(moves.split("U").length - 1 == moves.split("D").length - 1
//                && moves.split("R").length - 1 == moves.split("L").length - 1);
//        String[] sarr = moves.split("D");
//        for (String arr : sarr) {
//            System.out.println("--> "+arr);
//        }
//        System.out.println(moves.split("U").length);
//        String s = "嘎啦苹果新鲜整箱当季非冰糖心山西运城加力8月昭通非野生水果";
//        String result = "";
//        for (int i = 0; i < s.length(); i++) {
//            char mid = s.charAt(i);
//            if (mid != '非') {
//                result += mid;
//            } else if (mid == '非' && i < (s.length() / 2)) {
//                i = i + 2;
//            } else if (mid == '非' && i > (s.length() / 2)) {
//                break;
//            }
//
//        }
//        System.out.println(result);

//        String str = "这款波波水不腻不粘，味道也很好闻，补水效果不错。很好用，双十一买经济实惠";
//        System.out.println(ToAnalysis.parse(str));
        //Predictor<参数类型，返回值类型> 输入图片的话参数是Image
        //我的参数是float32 不要写成Double
//            Predictor<float[], Object> objectObjectPredictor = model.newPredictor(new NoBatchifyTranslator<float[], Object>() {
//                @Override
//                public NDList processInput(TranslatorContext translatorContext, float[] input) throws Exception {
//                    NDManager ndManager = translatorContext.getNDManager();
//                    NDArray ndArray = ndManager.create(input);
//                    //ndArray作为输入
//                    System.out.println(ndArray);
//                    return new NDList(ndArray);
//                }
//
//                @Override
//                public Object processOutput(TranslatorContext translatorContext, NDList ndList) throws Exception {
//                    System.out.println("process: " + ndList.get(0).getFloat());
//                    return ndList.get(0).getFloat();
//                }
//            });
//            [[101, 872, 1962, 2097, 1967, 1377, 809, 4500, 1408, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
        //首先创建一个模型
//        Model model = Model.newInstance("test");
        try {
            Path modeldir = Paths.get("D:\\1.pt");

//            model.load(Paths.get("D:\\multi_model.pt"));
//            System.out.println(model);
            NoBatchifyTranslator<NDList, NDList> translator = new NoBatchifyTranslator<NDList, NDList>() {
                @Override
                public NDList processInput(TranslatorContext ctx, NDList input) throws Exception {
                    NDManager ndManager = ctx.getNDManager();
                    NDArray ndArray = ndManager.create(String.valueOf(input));
                    System.out.println(ndArray);
                    return new NDList(ndArray);
                }

                @Override
                public NDList processOutput(TranslatorContext ctx, NDList ndList) throws Exception {
                    System.out.println("process: " + ndList);
                    System.out.println("process-1:" + ndList.get(0));
                    System.out.println("process-2:" + ndList.get(0).argMax());
                    return ndList;
                }
            };
            // 加载模型
            Criteria<NDList, NDList> criteria = Criteria.builder()
                    .setTypes(NDList.class, NDList.class)
                    .optModelPath(modeldir)
                    .optTranslator(translator)
                    .build();
            // 之后调用predictor，生成预测器
            Predictor<NDList, NDList> predictor = criteria.loadModel().newPredictor();


            System.out.println("=========");
            NDManager manager = NDManager.newBaseManager();
            int[] inLongArray = new int[]{101, 872, 1962, 2097, 1967, 1377, 809, 4500, 1408, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            NDArray nd = manager.create(inLongArray);
            NDList ndArrays = new NDList();
            ndArrays.add(nd);
            NDList predict = predictor.predict(ndArrays);

            System.out.println("predict: " + predict);
        } catch (Exception e) {
            System.out.println("11 ");
            e.printStackTrace();
        }
    }
}